﻿using SqlSugar;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace SqlSugarEncapsulation.Common
{
    public static class SqlSugarBaseExt
    {
        /// <summary>
        /// 获取列表
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="T2">目标类型</typeparam>
        /// <param name="repository"></param>
        /// <param name="sql">sql语句</param>
        /// <param name="parameters">参数</param>
        /// <returns></returns>
        public static T2 GetModel<T, T2>(this SqlSugarBase<T> repository, string sql, List<SugarParameter> parameters) where T : class, new()
        {
            return repository.connection.Ado.SqlQuery<T2>(sql, parameters).FirstOrDefault();
        }

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="T2">目标类型</typeparam>
        /// <param name="repository"></param>
        /// <param name="sql">sql语句</param>
        /// <param name="parameters">参数</param>
        /// <returns></returns>
        public static IEnumerable<T2> GetList<T, T2>(this SqlSugarBase<T> repository, string sql, List<SugarParameter> parameters) where T : class, new()
        {
            return repository.connection.Ado.SqlQuery<T2>(sql, parameters);
        }

        /// <summary>
        /// 获取DataReader
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="repository"></param>
        /// <param name="sql">sql语句</param>
        /// <param name="parameters">参数</param>
        /// <returns></returns>
        public static IDataReader GetDataReader<T>(this SqlSugarBase<T> repository, string sql, object parameters) where T : class, new()
        {
            return repository.connection.Ado.GetDataReader(sql, parameters);
        }

        /// <summary>
        /// 获取DataReader
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="repository"></param>
        /// <param name="sql">sql语句</param>
        /// <param name="parameters">参数</param>
        /// <returns></returns>
        public static IDataReader GetDataReader<T>(this SqlSugarBase<T> repository, string sql, params SugarParameter[] parameters) where T : class, new()
        {
            return repository.connection.Ado.GetDataReader(sql, parameters);
        }

        /// <summary>
        /// 获取DataReader
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="repository"></param>
        /// <param name="sql">sql语句</param>
        /// <param name="parameters">参数</param>
        /// <returns></returns>
        public static IDataReader GetDataReader<T>(this SqlSugarBase<T> repository, string sql, List<SugarParameter> parameters) where T : class, new()
        {
            return repository.connection.Ado.GetDataReader(sql, parameters);
        }
    }
}
